package com.acc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.acc.entity.Admin;
import com.acc.entity.EasyUIDataGridJSON;
import com.acc.entity.Msg;
import com.acc.entity.RoleName;
import com.acc.entity.RoleUri;
import com.acc.service.AdminService;
import com.acc.service.RoleService;
import com.acc.service.RoleUriService;
import com.acc.service.StandbyService;
import com.acc.service.impl.AdminServiceImpl;
import com.acc.service.impl.RoleServiceImpl;
import com.acc.service.impl.RoleUriServiceImpl;
import com.acc.service.impl.StandbyServiceImpl;
import com.acc.utils.Md5Utils;
import com.alibaba.fastjson.JSON;
/**
 * 
 * @author 朱春阳
 *
 */
public class AdminController extends InnerDispacherController2 {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 分页获取admin
	 * @param request
	 * @param response
	 */
	public void pageAdmin(HttpServletRequest request, HttpServletResponse response) {
		AdminService adminService = new AdminServiceImpl();
		Integer page = null;
		Integer rows = null;
		try {
			page = Integer.parseInt(request.getParameter("page"));
			rows = Integer.parseInt(request.getParameter("rows"));
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}

		Integer count = adminService.getAllSize();

		List<Admin> pageadmins = adminService.getPageAdmin(page, rows);

		EasyUIDataGridJSON json = new EasyUIDataGridJSON();
		json.setTotal(count);
		json.setRows(pageadmins);

		PrintWriter out = null;
		try {
			out = response.getWriter();
			response.setContentType("application/json");
			out.append(JSON.toJSONString(json));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			out.close();
		}

	}

	public void addAdmin(HttpServletRequest request, HttpServletResponse response) {
		AdminService adminService = new AdminServiceImpl();

		// 从输入框中取出值
		String roleName = request.getParameter("adminname");
		String pwd = request.getParameter("password");
		String password = null;
		if(pwd != null) {
			password = Md5Utils.md5(pwd);
		}
		// 增加记录
		boolean flag = adminService.addAdmin(roleName, password);

		// 存储判断的信息
		Msg msg = new Msg();
		String result = null;
		if (flag) {
			msg.setErrorMsg(null);
			msg.setSuccess(true);
			result = JSON.toJSONString(msg);
		} else {
			msg.setErrorMsg("增加失败");
			msg.setSuccess(false);
			result = JSON.toJSONString(msg);
		}

		// 将结果返回到页面上
		PrintWriter out = null;
		try {
			response.setContentType("application/json");
			out = response.getWriter();
			out.append(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	public void deleteAdmin(HttpServletRequest request, HttpServletResponse response) {
		// 从页面获取id
		String id_str = request.getParameter("adminId");
		Integer id = null;
		try {
			id = Integer.parseInt(id_str);
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}

		AdminService adminService = new AdminServiceImpl();
		System.out.println(id);
		// 判断删除是否成功
		boolean flag = adminService.deleteAdmin(id);

		// 存储判断的信息
		Msg msg = new Msg();
		String result = null;
		if (flag) {
			msg.setErrorMsg(null);
			msg.setSuccess(true);
			result = JSON.toJSONString(msg);
		} else {
			msg.setErrorMsg("删除失败");
			msg.setSuccess(false);
			result = JSON.toJSONString(msg);
		}

		response.setContentType("application/json");
		try {
			response.getWriter().append(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
